package classes {
	import flash.utils.getQualifiedClassName;

	public class XMLFunctions {
		public function XMLFunctions() {
			
		}
		
		public static function parseXML(_xml:String): Object {
			var xml:XML = new XML(_xml);
			return (new XMLFunctions()).toObject(xml);
		}
		
		private function toObject(_xml:XML):Object {
			var ret:Object = new Object;
			for (var a in _xml.children()) {
				var nodeName:String = _xml.children()[a].localName();

				if (typeof(ret[nodeName]) != "undefined" && getQualifiedClassName(ret[nodeName]) != "Array") {
					var tmp = ret[nodeName];
					ret[nodeName] = new Array();
					ret[nodeName].push(tmp);
					if (_xml.children()[a].children().length() > 1) {
						ret[nodeName].push(toObject(_xml.children()[a]));
					} else {
						ret[nodeName].push(parseXMLValue(_xml.children()[a].toString()));
					}
				} else if (getQualifiedClassName(ret[nodeName]) == "Array") {
					if (_xml.children()[a].children().length() > 1) {
						ret[nodeName].push(toObject(_xml.children()[a]));
					} else {
						ret[nodeName].push(parseXMLValue(_xml.children()[a].toString()));
					}
				} else {
					if (_xml.children()[a].children().length() > 1) {
						ret[nodeName] = toObject(_xml.children()[a]);
					} else {
						ret[nodeName] = parseXMLValue(_xml.children()[a].toString());
					}
				}
			}
			return ret;
		}
		public function parseXMLValue(value:String):Object {
			if (parseFloat(value).toString() == value)
				return parseFloat(value);
			else if (value == "false")
				return false;
			else if (value == "true")
				return true;
			return value;
		}
	}
}